<?php
namespace App\Repositories;


use App\Interfaces\PushTarrifService;
use App\Models\Order;
use App\Models\PushTarrif;

use DB,Log;

/*
 * 推关订单服务
 * 
 * @date:2016/03/17
 * @author:Leen
 */
class PushTarrifRepository implements PushTarrifService{


    /*
     * 创建推关订单表的记录
     * @param order_id:订单id
     * @param order_sn:订单单号
     * @param push_tarrif_status:订单推关状态：1、可推关；2、已推关；3、已取消；4、推关失败
     * @param $reasion:推单失败
     *
     * @date:2016/03/17
     * @author:Leen
     */
    public function createPushTarrif($order_id, $order_sn, $push_tarrif_status, $reasion=''){
        $add_time = date('Y-m-d H:i:s', time());   //可推关的时间
        $id = PushTarrif::insertGetId(['order_id' => trim($order_id), 'order_sn' =>trim($order_sn), 'push_tarrif_status' =>trim($push_tarrif_status), 'add_time' =>trim($add_time), 'reasion' =>trim($reasion)]);

        return $id;

    }//end func createPushTarrif

    /*
     * 根据推关订单表的id查询
     * @param id:推关订单表的id
     *
     * @date:2016/03/17
     * @author:Leen
     */
    public function getById($id){

    }//end func getById

    /*
     * 根据订单id查询
     * @param order_id:订单id
     *
     * @date:2016/03/17
     * @author:Leen
     */
    public function getByOrderId($order_id){
        return PushTarrif::where('order_id', $order_id)->first();
    }//end func getByOrderId

    /*
     * 根据订单单号查询
     * @param order_sn:订单单号order_sn
     *
     * @date:2016/03/17
     * @author:Leen
     */
    public function getByOrderSn($order_sn){

    }//end func getByOrderSn


    /*
     * 根据订单id修改推关订单状态
     * @param order_id:订单id
     * @param push_tarrif_status:推关订单状态，1、可推关；2、已推关；3、已取消；4、推关失败
     * @param describe:失败原因
     *
     * @date:2016/03/17
     * @author:Leen
     */
    public function updatePushTarrif($order_id, $push_tarrif_status, $pro_time, $reasion=''){
        $data = [];
        $data['push_tarrif_status'] = $push_tarrif_status;
        if(!empty($reasion)){

            $data['reasion'] = $reasion;
        }//end if()

        //$pro_time = date('Y-m-d H:i:s', time());   //当前时间

        switch($push_tarrif_status){
            case 1:
                $data['add_time'] = $pro_time;
                break;
            case 2:
                $data['success_time'] = $pro_time;
                break;
            case 3:
                $data['cacel_time'] = $pro_time;
                break;
            case 4:
                $data['fail_time'] = $pro_time;
                break;
        }

        //PushTarrif::where('order_id', $order_id)->update(['push_tarrif_status'=>$push_tarrif_status, 'reasion' =>trim($reasion)]);
        PushTarrif::where('order_id', $order_id)->update($data);

        return true;
    }//end func updatePushTarrif






}